/*
 * litchicms litchi.js
 * Copyright (C) 2010-6-18 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

function getlocation(){
    str=window.location+"";
    if(str.indexOf('#')>0){
        return str.substr(0,str.indexOf('#'))
    }
    
    return str;
}

/* 附件上传系列 */
function upload_add_file(i){
    var allfile=$('allfile');

    if(allfile.getElementsByTagName('div').length+i>10){
        alert("一次最多只能上传10个文件\n如果有太多请分次..");
        return;
    }
    

    for(var j=0;j<i;j++){
        var temp=document.createElement('div');
        temp.className='onefile';
        temp.innerHTML="<input type='file' name='attachment_files[]' onchange='upload_onchange(this)'/>&nbsp;&nbsp;附件名:&nbsp;<input type='text' name='attachment_description[]' class='input_text_up' />&nbsp;&nbsp;<input type='button' onclick='upload_del_file(this)' class='buttonup' value='删除'/><input type='hidden' name='attachment_suffix[]' value=''/>";
        allfile.appendChild(temp);
    }
}
function upload_del_file(obj){
    var all=obj.parentNode.parentNode;
    all.removeChild(obj.parentNode);

    //擦了 破IE  后面的代码为你写的
    if(Browser.ie){
        all=all.getElementsByTagName('div');
        for(var i =0 ;i<all.length;i++){
            all[i].setAttribute('class','onefile');
        }
    }
}
function upload_onchange(obj){
    var filename=obj.value;
    filename=filename.replace(/\\/g,"/");
    for(;;){
        var $temp=filename.indexOf('/');
        if($temp==-1){
            break;
        }else{
            filename=filename.substr($temp+1,filename.length-$temp);
        }

    }
    var suffix=filename;
    for(;;){
        var $temp=suffix.indexOf('.');
        if($temp==-1){
            break;
        }else{
            suffix=suffix.substr($temp+1,suffix.length-$temp);
        }

    }
    filename=filename.substr(0,filename.length-suffix.length-1);
    var brothers=obj.parentNode.getElementsByTagName('input');
    for(var i =0 ; i<brothers.length;i++){
        if(brothers[i].getAttribute('name')=='attachment_description[]'){
            brothers[i].value=filename;
        }
        if(brothers[i].getAttribute('name')=='attachment_suffix[]'){
            brothers[i].value=suffix;
        }
    }
    
}

function upload_submit(obj){
    show_pop();
    var targetbak=obj.target;
    obj.target='uploadtarget';
    obj.method='post';
    obj.encoding='multipart/form-data';
    obj.action=getlocation()+'/uploadfile-yes';
    
    $('brk_upload_form').style.display='none';
    $('brk_uploading').style.display='block';
    obj.submit();
    obj.encoding= 'application/x-www-form-urlencoded';
    obj.target=targetbak;
    obj.action=window.location;
    
}

function  upload_iframe_onload(obj){
    dom=obj.contentDocument || obj.contentWindow.document;
    $('brk_uploading').style.display='none';
    if(dom.body.innerHTML!=''){
        $('brk_upload_tip').style.display='block';
        $('brk_upload_tip').innerHTML=dom.body.innerHTML+'<br/><br/><input onclick="goon_upload_handle()" value="继续上传" id="goon_upload" type="button" />';
        for(;;){
            if($('allfile').$$('div').length>0){
                $('allfile').removeChild($('allfile').$$('div')[0]);
            }else{
                break;
            }
        }
        $('allfile').innerHTML="<div class='onefile'><input type='file' name='attachment_files[]' onchange='upload_onchange(this)'/>&nbsp;&nbsp;附件名:&nbsp;<input type='text' name=\"attachment_description[]\" class=\"input_text_up\" /><input type='hidden' name='attachment_suffix[]' value=''/></div>";
    }
}

function show_hide_upload_form(obj){
    if($('attachment_upload').offsetHeight<40){
        $('attachment_upload').style.height='auto';
        obj.value='隐藏上传表单'
    }else{
        $('attachment_upload').style.height='30px';
        obj.value='显示上传表单'
    }
}

function goon_upload_handle(){
    $('brk_upload_tip').style.display='none';
    $('brk_upload_form').style.display='block';
}

ATTACHMENT_fun_Plugins=[];
function upload_insert_to_select(obj){
    for(var i=0;;i++){
        img=obj[i];
        if(img==undefined){
            break;
        }
        $('attachment_has').innerHTML+="<input type=\"hidden\" name=\"Component[attachment][ids][]\" id=\"attachment_has_hidden_input_"+img.attach_id+"\" value=\""+img.attach_id+"\"/>";

        var str_del="<a href='#' onclick=\"attachment_del("+img.attach_id+");return false\">删除</a>";
        var str_changename="<a href='#' onclick=\"attachment_changename("+img.attach_id+");return false\">更名</a>";
        var str_cut="<a href='#' onclick=\"attachment_cut('"+img.attach_path+"',"+img.attach_id+");return false\">裁切</a>";
        var str_zoom="<a href='#' onclick=\"attachment_zoom('"+img.attach_path+"',"+img.attach_id+");return false\">调整缩略图</a>";
        var str_insert_big="<a href='#' onclick=\"attachment_insert('"+img.attach_path+"','"+img.attach_name+"');return false\">插入大图</a>";
        var str_insert_small="<a href='#' onclick=\"attachment_insert('"+img.attach_thumb+"','"+img.attach_name+"');return false\" id='attachment_oneimg_thumb_"+img.attach_id+"'>插入缩略图</a>";
        var str_insert_attach="<a href='#' onclick=\"attachment_insert('"+ROOT+"data/sys/attach_20_20.jpg','"+img.attach_name+"',"+img.attach_id+",'"+img.attach_type+"');return false\">插入附件</a>";
        var str_insert_gif="<a href='#' onclick=\"attachment_insert('"+img.attach_thumb+"','"+img.attach_name+"');return false\">插入GIF</a>";
        if(img.attach_type=='.jpg' || img.attach_type=='.png'){
            attachHTML=str_del+" | "+str_changename+" | "+str_cut+" | "+str_zoom+" | "+str_insert_big+" | "+str_insert_small+" | "+str_insert_attach;
        }else if(img.attach_sys_suffix==".attach"){
            attachHTML=str_del+" | "+str_changename+" | "+str_insert_attach;
        }else if(img.attach_type=='.gif'){
            attachHTML=str_del+" | "+str_changename+" | "+str_insert_gif+" | "+str_insert_attach;
        }

        
        for(var j=0 ; j<ATTACHMENT_fun_Plugins.length ; j++){
            $temp=ATTACHMENT_fun_Plugins[j](img);
            attachHTML+=$temp?" | "+$temp:"";
        }

        

        var one=document.createElement('div');
        one.className='attachment_oneimg_div';
        one.id="attachment_oneimg_div_"+img.attach_id;
        one.innerHTML="<table>"+
        "<tr>"+
        "<td class='attachment_oneimg_div_img'><img src='"+img.attach_sys_thumb+"' id='attachment_oneimg_40_img_"+img.attach_id+"'  alt='"+img.attach_name+"'/></td>"+
        "<td class='texttop'>&nbsp;<span class='red'>"+img.attach_type+"</span>&nbsp;<span id='attachment_one_name_span_"+img.attach_id+"'><span>"+img.attach_name+"</span></span><br/>&nbsp;"+attachHTML+"</td>"+
        "</tr>"+
        "</table>";
        $('attachment_private').appendChild(one);
    }

    img='';
    attachHTML='';
}


//改名
function attachment_changename(attach_id){
    var ev=window.event || arguments.callee.caller.arguments[0];
    var target= ev.target||ev.srcElement;
    try{
        var oldname=$('attachment_one_name_span_'+attach_id).$$('span')[0].innerHTML;
        $('attachment_one_name_span_'+attach_id).innerHTML="<input type='text' class='input_text_4' value='"+oldname+"' />";
        target.innerHTML="确定更名";
    }catch(e){
        try{
            var newname=$('attachment_one_name_span_'+attach_id).$$('input')[0].value;
            var OP={
                method:'post',
                parametes:"attachment_id="+attach_id+"&newname="+newname,
                onS:function(t){
                    try{
                        $a=jsonstr2obj(t.responseText);
                        if($a.status){
                            $('attachment_one_name_span_'+attach_id).innerHTML="<span>"+newname+"</span>";
                            target.innerHTML="更名";
                        }
                    }catch(e){

                    }
                }
            }
            tAjax(window.parent.getlocation()+'/attachmentchangename-yes',OP);
        }catch(e){}
    }
}

//裁切
function attachment_cut(attach_path,attach_id){
    tinyMCE.get('textareaEditor').execCommand('mceBrkimgcut',false,[attach_path,attach_id]);
}

//缩放
function attachment_zoom(attach_path,attach_id){
    tinyMCE.get('textareaEditor').execCommand('mceBrkimgzoom',false,[attach_path,attach_id]);
}
//tinyMCE.get('textareaEditor')
//删除
function attachment_del(id){
    if(window.confirm("您确定要删除么? \n\n\n\n\n\n\n\n不可恢复哦")){
        var OP={
            method:'post',
            parametes:"attachment_id="+id,
            onS:function(t){
                try{
                    $a=jsonstr2obj(t.responseText);
                    if($a.status){
                        $('attachment_private').removeChild($("attachment_oneimg_div_"+id));
                        $('attachment_has').removeChild($("attachment_has_hidden_input_"+id));
                    }
                }catch(e){
                    
                }
                
            }
        }
        tAjax(getlocation()+'/deletefile-yes',OP);
    }
}

//插入
function attachment_insert(url,alt,attach_id,attach_type){
    if(attach_id){
        tinyMCE.execCommand('mceInsertContent',false,'<a href="'+ROOT+'index.php?attach-'+attach_id+'"><img src="'+url+'" />'+alt+attach_type+'</a>');
    }else{
        tinyMCE.execCommand('mceInsertContent',false,'<img src="'+url+'" alt="'+alt+'"  title="'+alt+'" />');
    }
    
}

//重写系统40_40缩略图
function _sys_40($id,$img){
    $('attachment_oneimg_40_img_'+$id).src=$img+"?"+new Date().getTime();
}
//更新插入缩略图的URL
function _sys_reSetThumb($id,$img,$attach_name){
    $('attachment_oneimg_thumb_'+$id).onclick=function(){
        attachment_insert($img+"?"+new Date().getTime(),$attach_name);
    }
}
//这段代码写得好戳
function upload_change_box(i){
    $('upload_tabar_'+1).style.borderBottom='1px solid orange';
    $('upload_tabar_'+2).style.borderBottom='1px solid orange';
    $('upload_tabar_'+1).setAttribute('class','');
    $('upload_tabar_'+2).setAttribute('class','');

    $('upload_tabar_'+i).style.borderBottom='1px solid #fafafa';
    $('upload_tabar_'+i).setAttribute('class','current');

    $('file_upload_box_'+1).style.display='none';
    $('file_upload_box_'+2).style.display='none';
    $('file_upload_box_'+i).style.display='block';
    $('upload_tabar_'+i).blur();
}




/* 模板标签调用系列 */
function template_show_calltag(str){
    if($(str)){
        if($(str).style.display==''){
            $(str).style.display='none';
        }else{
            $(str).style.display='';
        }
    }else{
        
}
}




/* LIST页面 */
function list_submit(obj){
    obj.submit();
    GLOBAL['waittime']=0;
    show_pop();
    waittime();
}

function list_edit_response(obj){
    if(waittimesetTimeout){
        clearTimeout(waittimesetTimeout);
    }
    var innerHTML="<div class='textleft m10 ML20'>";
    var jump="";
    for(var i =0 ; i<obj[1].length;i++){
        innerHTML+="<span class='red'>"+obj[1][i]+"</span><br/>";
    }
    innerHTML+="</div>";
    if(obj[0]==0){
        jump="<a href='#' onclick=\"hide_pop();\"  style='color:#F00' id='msgs_goto_link'>继续</a>";
    }else{
        jump="<a href='#' onclick=\"window.location='?"+obj[2][1]+"'\" style='color:#F00' id='msgs_goto_link'>继续</a>";
    }
    innerHTML+="<div class='textleft m10 ML20'><span id='msgs_goto'>3</span>&nbsp;秒后"+jump+"</div>";
    show_pop(innerHTML);
    list_edit_response_goto();
}

function list_edit_response_goto(){
    $msgs=$("msgs_goto");
    if(parseInt($msgs.innerHTML)==0){
        clearTimeout(list_edit_response_goto_Timeout);
        if(Browser.ie){
            $('msgs_goto_link').click();
        }else if(Browser.firefox || Browser.chrome ){
            var ev = document.createEvent("MouseEvents");
            ev.initEvent("click",true,true);
            $('msgs_goto_link').dispatchEvent(ev);
        }else{
            alert("说实话,我还不确定你现在使用的浏览器是否支持某某事件模型,所以你需要手动点击'继续',..可以发信件到我邮箱...,对了我的邮箱539247@qq.com,尽情鄙视我吧,尽管用QQ的邮箱很不专业")
        }
    }else{
        $msgs.innerHTML=parseInt($msgs.innerHTML)-1;
        list_edit_response_goto_Timeout=setTimeout(list_edit_response_goto,1000);
    }
}




/*等待时间*/
function waittime(){
    GLOBAL['waittime']++;
    if(GLOBAL['waittime']>20){
        clearTimeout(waittimesetTimeout);
        GLOBAL['_pop'].innerHTML+="<br/>你等待的时间过长,可能是因为程序发生了错误,如果需要请联系开发人员";
    }else{
        waittimesetTimeout=setTimeout(waittime,1000);
    }
}

/*POP*/
function show_pop(innerHTML){
    if(!innerHTML){
        innerHTML="<img src='system/litchiblack/images/loading.gif'/><br/>处理中...";
    }
    getBrowserInfo();
    if(GLOBAL['_pop_bg']){
        GLOBAL['_pop_bg'].style.display="";
        GLOBAL['_pop'].style.display="";
        GLOBAL['_pop'].style.left=(Browser.cWidth-300)/2+"px";
        GLOBAL['_pop'].style.top=Browser.sTop+200+"px";
        GLOBAL['_pop'].innerHTML=innerHTML;
    }else{
        GLOBAL['_pop_bg']=document.createElement('div');
        GLOBAL['_pop_bg'].style.cssText="background-color:#eee;z-index:1000;position: absolute; left: 0;top: 0;opacity:0.1;filter:alpha(opacity=10);";
        GLOBAL['_pop_bg'].style.width=Browser.cWidth+'px';
        GLOBAL['_pop_bg'].style.height=Browser.sHeight*2+'px';
        document.body.appendChild(GLOBAL['_pop_bg']);

        GLOBAL['_pop']=document.createElement('div');
        GLOBAL['_pop'].style.cssText="z-index:1001;position: absolute; left: 0;top: 0;width:300px; line-height:20px;border:2px solid #888;background-color:#eee";
        GLOBAL['_pop'].style.left=(Browser.cWidth-300)/2+"px";
        GLOBAL['_pop'].style.top=Browser.sTop+200+"px";
        GLOBAL['_pop'].innerHTML=innerHTML;
        document.body.appendChild(GLOBAL['_pop']);
        window.onscroll=scroll_pop;
    }
}
function hide_pop(){
    GLOBAL['_pop'].style.display="none";
    GLOBAL['_pop_bg'].style.display="none";
}
function scroll_pop(){
    getBrowserInfo();
    GLOBAL['_pop'].style.top=Browser.sTop+200+"px";
}


/* 自动一行的高度  */
function data_row_auto_height_show(obj){
    data_row_auto_height_hide(nextSibling(obj));
    var divs=$$(obj,'div');
    for(var i = 0 ; i <divs.length; i++){
        if(divs[i].getAttribute('data_row_auto_height')){
            divs[i].style.height='auto';
        }
    }
}
function data_row_auto_height_hide(obj){
    if(obj){
        var divs=$$(obj,'div');
        for(var i = 0 ; i <divs.length; i++){
            if(divs[i].getAttribute('data_row_auto_height')){
                divs[i].style.height='20px';
            }
        }
        data_row_auto_height_hide(nextSibling(obj));
    }
}



/* 页面域变量 */
function show_hide_pagevarset(obj){
    if($('show_hide_pagevarset').style.display!='block'){
        $('show_hide_pagevarset').style.display='block';
        obj.value='隐藏'
    }else{
        $('show_hide_pagevarset').style.display='none';
        obj.value='显示'
    }
}



/*图片按比例缩放*/
_zoom=function(w,h,tow,toh){
    var x=w/tow;
    var y=h/toh;
    var xy=1;
    if(x>=1 || y>=1){
        if(x>y){
            w=tow;
            h=parseInt(h/x);
            xy=x;
        }else{
            h=toh;
            w=parseInt(w/y);
            xy=y;
        }
    }
    return [w,h,xy];
}